<script setup>
import PageContainer from "@/components/PageContainer.vue";
import { ref } from "vue";
import { artDelChannelService, artGetChannelsService } from "@/api/article";
import { Delete, Edit } from "@element-plus/icons-vue";
import ChannelEdit from "@/views/article/components/ChannelEdit.vue"
import { ElMessage } from "element-plus";
import { ElMessageBox } from "element-plus";
const channelList = ref([])
const loading = ref(false)
const dialog = ref({})
const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelsService();
  channelList.value = res.data
  loading.value = false
}
getChannelList()


const onEditChannel = (row) => {
  // console.log(row);
  // console.log($index);
  dialog.value.open(row)
}

const onDelChannel = (row) => {
  ElMessageBox.confirm("你确认要删除该分类吗?", "温馨提示", {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  }).then(async () => {
    await artDelChannelService(row.id)
    ElMessage.success('删除成功')
    getChannelList()
  })

}

const onAddChannel = () => {
  dialog.value.open({})
}

const onSuccess = () => {
  getChannelList()
}


</script>

<template>
  <PageContainer title="文章分类">
    <template #extra>
      <el-button @click="onAddChannel">添加分类</el-button>
    </template>
    <el-table :data="channelList" style="width: 100%;" v-loading="loading">
      <el-table-column label="序号" width="100" type="index" align="center"></el-table-column>
      <el-table-column label="分类名称" prop="cateName" align="center"></el-table-column>
      <el-table-column label="分类别名" prop="cateAlias" align="center"></el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <!-- row是每一项，$index是下标 -->
        <template #default="{ row, $index }">
          <el-button :icon="Edit" @click="onEditChannel(row, $index)" type="primary" circle plain></el-button>
          <el-button :icon="Delete" @click="onDelChannel(row, $index)" type="danger" circle plain></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <ChannelEdit ref="dialog" @success="onSuccess">

    </ChannelEdit>


  </PageContainer>
</template>

<style scoped></style>
